﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SubSonic;
using CXDB;
using Coolite.Ext.Web;

public partial class Provider_View : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Store1Bind();
        }
    }
    protected void Store1_OnRefreshData(object s, StoreRefreshDataEventArgs e) 
    {
        Store1Bind();
    }
    protected void Store1Bind()
    {
        Query q = new Query(VwProviderRep.Schema);
        switch (Request["type"])
        {
            case "dept":
                q.AddWhere("Dept_Master_id", Cookies.getCookies("cUId"));
                q.AddWhere("dept_master_status", SubSonic.Comparison.NotEquals, 2);
                //Panel7.Collapsed = false;
                //Panel8.Visible = false;
                //Panel9.Visible = false;
                //Panel10.Visible = false;
                Btnboss.Visible = false;
                Btntechmaster.Visible = false;
                Btnofficemaster.Visible = false;
                
                tfboss_remark.Disabled = true;
                tfboss_status.Disabled = true;
                tftechmaster_remark.Disabled = true;
                tftechmaster_status.Disabled = true;
                tfofficemaster_remark.Disabled = true;
                tfofficemaster_status.Disabled = true;

                break;
            case "tech":
                q.AddWhere("tech_Master_id", Cookies.getCookies("cUId"));
                q.AddWhere("dept_master_status", 1);
                q.AddWhere("tech_master_status", 0);
                Panel8.Collapsed = false;
                Panel7.Visible = false;
                Panel9.Visible = false;
                Panel10.Visible = false;
                tfdeptmaster_remark.Disabled = true;
                tfdeptmaster_status.Disabled = true;
                tfofficemaster_id.Disabled = true;
                tfofficemaster_remark.Disabled = true;
                tfofficemaster_status.Disabled = true;
                tftechmaster_id.Disabled = true; 
                tfboss_id.Disabled = true;
                tfboss_remark.Disabled = true;
                tfboss_status.Disabled = true;
                break;
            case "office":
                q.AddWhere("office_Master_id", Cookies.getCookies("cUId"));
                q.AddWhere("dept_master_status", 1);
                q.AddWhere("office_master_status", 0);
                Panel9.Collapsed = false;
                Panel8.Visible = false;
                Panel7.Visible = false;
                Panel10.Visible = false;
                tfdeptmaster_remark.Disabled = true;
                tfdeptmaster_status.Disabled = true;
                tfofficemaster_id.Disabled = true;
                 
                tftechmaster_id.Disabled = true;
                tftechmaster_remark.Disabled = true;
                tftechmaster_status.Disabled = true;
                tfboss_id.Disabled = true;
                tfboss_remark.Disabled = true;
                tfboss_status.Disabled = true;
                break;
            case "boss":
                q.AddWhere("boss_id", Cookies.getCookies("cUId"));
                q.AddWhere("dept_master_status", 1);
                q.AddWhere("tech_master_status", 1);
                q.AddWhere("office_master_status", 1);
                q.AddWhere("boss_status", SubSonic.Comparison.NotEquals, 2);
                //Panel10.Collapsed = false;
                //Panel8.Visible = false;
                //Panel9.Visible = false;
                //Panel7.Visible = false;
                tfdeptmaster_remark.Disabled = true;
                tfdeptmaster_status.Disabled = true;
                tfofficemaster_id.Disabled = true;
                tfofficemaster_remark.Disabled = true;
                tfofficemaster_status.Disabled = true;
                tftechmaster_id.Disabled = true;
                tftechmaster_remark.Disabled = true;
                tftechmaster_status.Disabled = true;
                tfboss_id.Disabled = true;
                Btndeptmaster.Visible = false;
                Btntechmaster.Visible = false;
                Btnofficemaster.Visible = false;
                break;
            default:
                break;
        }
        
        q.SetSelectList("id as pid,providername,devicename,Dept_master_status,Tech_master_status,Office_master_status,Boss_status,deptname,username");
        q.ORDER_BY("id desc");
        Store1.DataSource = q.ExecuteDataSet();
        Store1.DataBind();

    }
    protected void RowSelect(object s,AjaxEventArgs e)
    {
        string tpid = e.ExtraParams["pid"];
        pid.Value = tpid;
        Query q = new Query(CxProvider.Schema);
        q.AddWhere("id", tpid);
        IDataReader dr = q.ExecuteReader();
        if(dr.Read())
        {
            guid.Value = dr["guid"].ToString();

            string file = CXFileUpload.show(new Guid(dr["guid"].ToString()));
            string[] str = file.Split('$');
            tfFileShow.NavigateUrl = str[0];
            tfFileShow.Text = str[1];
            
            tfprovidername.Text = dr["providername"].ToString();
            tfdevicename.Text = dr["devicename"].ToString();
            tfsham_time.Text = dr["sham_time"].ToString();
            tfsham_project.Text = dr["sham_project"].ToString();
            tfsham_detail.Text = dr["sham_detail"].ToString();
            tfxinghui_time.Text = dr["xinghu_time"].ToString();
            tfxinghui_project.Text = dr["xinghu_project"].ToString();
            tfxinghui_detail.Text = dr["xinghu_detail"].ToString();
            tfcontract_time.Text = dr["contract_time"].ToString();
            tfcontract_project.Text = dr["contract_project"].ToString();
            tfcontract_detail.Text = dr["contract_detail"].ToString();
            tfother_detail.Text = dr["other_detail"].ToString();

            if (dr["dept_master_status"].ToString() == "1" &&   dr["tech_master_status"].ToString()=="1" &&  dr["boss_status"].ToString()=="1")
            {
                Btndeptmaster.Hidden = true;
                Btnofficemaster.Hidden = true;
                Btntechmaster.Hidden = true;
                Btnboss.Hidden = true;
            }
            //else
            //{
            //    //部门领导
            //    Btndeptmaster.Hidden = (Cookies.getCookies("cuid") != dr["dept_master_id"].ToString());
            //}
            Btndeptmaster.Hidden = (Cookies.getCookies("cuid") != dr["dept_master_id"].ToString());
            tfdeptmaster_remark.Text = dr["dept_master_remark"].ToString();
            tfdeptmaster_status.SetInitValue(dr["dept_master_status"].ToString());
 
            //生产技术部领导 
            tftechmaster_id.Reset();
            Store_Databind(Store2, 5);
            if(dr["tech_master_id"].ToString()=="0")
            {
                tftechmaster_remark.Text = "";
            }
            else
            {
                Btntechmaster.Disabled = (Cookies.getCookies("cuid") != dr["tech_master_id"].ToString());
                tftechmaster_id.SetInitValue(dr["tech_master_id"].ToString());
                tftechmaster_status.SetInitValue(dr["tech_master_status"].ToString());
                tftechmaster_remark.Text = dr["tech_master_remark"].ToString();
            }
            
            //办公室领导
            tfofficemaster_id.Reset();
            Store_Databind(Store3,2);
            if(dr["office_master_id"].ToString()=="0")
            {
                tfofficemaster_remark.Text = "";
            }
            else
            {
                Btnofficemaster.Disabled=(Cookies.getCookies("cuid")!=dr["office_master_id"].ToString());
                tfofficemaster_id.SetInitValue(dr["office_master_id"].ToString());
                tfofficemaster_status.SetInitValue(dr["office_master_status"].ToString());
                tfofficemaster_remark.Text = dr["office_master_remark"].ToString();
            }
            
            //公司领导
            tfboss_id.Reset();
            Store_Databind(Store4,1);
            if(dr["boss_id"].ToString()=="0")
            {
                tfboss_remark.Text = "";
            }
            else
            {
                Btnboss.Disabled = (Cookies.getCookies("cuid") != dr["boss_id"].ToString());
                tfboss_id.SetInitValue(dr["boss_id"].ToString());
                tfboss_status.SetInitValue(dr["boss_status"].ToString());
                tfboss_remark.Text = dr["boss_remark"].ToString();
            }
        }
        
    }
    protected void Store_Databind(Store s,int deptid)
    {
        Query q = new Query(CxUserBase.Schema);
        q.AddWhere("depid", deptid);
        q.AddWhere("levelid", 1);
        q.SetSelectList("uid,username");
        s.DataSource = q.ExecuteDataSet();
        s.DataBind();
    }
    
    protected void Btndeptmaster_click(object s,AjaxEventArgs e)
    {
        
        
        if (tfdeptmaster_remark.Text.Trim() == "") { Err.Warning("审核意见不能为空！",MessageBox.Icon.NONE); return; }
        if (tfdeptmaster_status.SelectedIndex == -1) { Err.Warning("请选择审核态度！", MessageBox.Icon.WARNING); return; }
        
        
        CxProvider cx = new CxProvider("id", pid.Value);

        if (tfdeptmaster_status.SelectedItem.Value != "2")
        {
            if (tfofficemaster_id.SelectedIndex == -1) { Err.Warning("请选择办公室领导！", MessageBox.Icon.WARNING); return; }
            if (tfboss_id.SelectedIndex == -1) { Err.Warning("请选择公司领导！", MessageBox.Icon.WARNING); return; }
            if (tftechmaster_id.SelectedIndex == -1) { Err.Warning("请选择生产技术部领导！", MessageBox.Icon.WARNING); return; }
            cx.TechMasterId = int.Parse(tftechmaster_id.SelectedItem.Value);
            cx.BossId = int.Parse(tfboss_id.SelectedItem.Value);
            cx.OfficeMasterId = int.Parse(tfofficemaster_id.SelectedItem.Value);

            cx.TechMasterStatus = 0;
            cx.OfficeMasterStatus = 0;
            cx.BossStatus = 0;
        } 
        cx.DeptMasterStatus = int.Parse(tfdeptmaster_status.SelectedItem.Value);
        cx.DeptMasterTime = DateTime.Now;
        cx.DeptMasterRemark = tfdeptmaster_remark.Text;
        cx.Save();
        //增加日志记录
        LogRecord.Add(new Guid(guid.Value.ToString()), int.Parse(Cookies.getCookies("cuid")), tfdeptmaster_remark.Text,
                      int.Parse(tfdeptmaster_status.SelectedItem.Value));
        ClearForm();
        Err.Warning("已提交审核意见！",MessageBox.Icon.NONE);

    }
    protected void Btntechmaster_click(object s, AjaxEventArgs e)
    {
        if (tftechmaster_remark.Text.Trim() == "") { Err.Warning("审核意见不能为空！", MessageBox.Icon.WARNING); return; }
        if (tftechmaster_status.SelectedIndex == -1) { Err.Warning("请选择审核态度！", MessageBox.Icon.WARNING); return; }
        CxProvider cx = new CxProvider("id", pid.Value);
        cx.TechMasterRemark = tftechmaster_remark.Text;
        cx.TechMasterStatus = int.Parse(tftechmaster_status.SelectedItem.Value);
        cx.TechMasterTime = DateTime.Now;
        cx.Save();
        //增加日志记录
        LogRecord.Add(new Guid(guid.Value.ToString()), int.Parse(Cookies.getCookies("cuid")), tftechmaster_remark.Text,
                      int.Parse(tftechmaster_status.SelectedItem.Value));
        ClearForm();
        Err.Warning("批复成功！", MessageBox.Icon.NONE);

    }

    protected void Btnofficemaster_click(object s, AjaxEventArgs e)
    {
        if (tfofficemaster_remark.Text.Trim() == "") { Err.Warning("审核意见不能为空！", MessageBox.Icon.WARNING); return; }
        if (tfofficemaster_status.SelectedIndex == -1) { Err.Warning("请选择审核态度！", MessageBox.Icon.WARNING); return; }
        CxProvider cx = new CxProvider("id", pid.Value);
        cx.OfficeMasterRemark = tfofficemaster_remark.Text;
        cx.OfficeMasterStatus = int.Parse(tfofficemaster_status.SelectedItem.Value);
        cx.OfficeMasterTime = DateTime.Now;
        cx.Save();
        //增加日志记录
        LogRecord.Add(new Guid(guid.Value.ToString()), int.Parse(Cookies.getCookies("cuid")), tfofficemaster_remark.Text,
                      int.Parse(tfofficemaster_status.SelectedItem.Value));
        ClearForm();
        Err.Warning("批复成功！",MessageBox.Icon.NONE);
    }

    protected void Btnboss_click(object s, AjaxEventArgs e)
    {
        if (tfboss_remark.Text.Trim() == "") { Err.Warning("审核意见不能为空！", MessageBox.Icon.WARNING); return; }
        if (tfboss_status.SelectedIndex == -1) { Err.Warning("请选择审核态度！", MessageBox.Icon.WARNING); return; }
        CxProvider cx = new CxProvider("id", pid.Value);
        cx.BossRemark = tfboss_remark.Text;
        cx.BossStatus = int.Parse(tfboss_status.SelectedItem.Value);
        cx.BossTime = DateTime.Now;
        cx.Save();

        //增加日志记录
        LogRecord.Add(new Guid(guid.Value.ToString()), int.Parse(Cookies.getCookies("cuid")), tfboss_remark.Text,
                      int.Parse(tfboss_status.SelectedItem.Value));
        ClearForm();
        Err.Warning("批复成功！", MessageBox.Icon.NONE);
    }

    protected void ClearForm()
    {
        guid.Clear();
        tfxinghui_time.Text = "";
        tfxinghui_project.Text = "";
        tfxinghui_detail.Text = "";
        //tftechmaster_time.Text = "";
        tftechmaster_status.Reset();
        tftechmaster_remark.Text = "";
        tftechmaster_id.Reset();
        tfsham_time.Text = "";
        tfsham_project.Text = "";
        tfsham_detail.Text = "";
        tfprovidername.Text = "";
        tfother_detail.Text = "";
        //tfofficemaster_time.Text = "";
        tfofficemaster_status.Reset();
        tfofficemaster_remark.Text = "";
        tfofficemaster_id.Reset();
        tfFileShow.Text = "";
        tfdevicename.Text = "";
        //tfdeptmaster_time.Text = "";
        tfdeptmaster_status.Reset();
        tfdeptmaster_remark.Text = "";
        tfcontract_time.Text = "";
        tfcontract_project.Text = "";
        tfcontract_detail.Text = "";
       // tfboss_time.Text = "";
        tfboss_status.Reset();
        tfboss_remark.Text = "";
        tfboss_id.Reset();
    }
}
